package com.gzy.literature.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gzy.literature.dao.UserRecommendationsDao;
import com.gzy.literature.entity.UserRecommendations;
import com.gzy.literature.service.UserRecommendationsService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;


@Service("userRecommendationsService")
public class UserRecommendationsServiceImpl implements UserRecommendationsService {

    @Resource
    private UserRecommendationsDao userRecommendationsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public UserRecommendations queryById(Integer userId) {
        return this.userRecommendationsDao.queryById(userId);
    }
    /**
     * 分页查询
     *
     * @param userRecommendations 筛选条件（可以为null或包含部分字段用于筛选）
     * @param pageNum            页码（从1开始）
     * @param pageSize           每页显示的记录数
     * @return 分页查询结果
     */
    @Override
    public PageInfo<UserRecommendations> queryCll(UserRecommendations userRecommendations, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserRecommendations> list = this.userRecommendationsDao.queryCll(userRecommendations);
        return new PageInfo<>(list);
    }
    /**
     * 新增数据
     *
     * @param userRecommendations 实例对象
     * @return 实例对象（通常包含数据库生成的ID或其他字段）
     */
    @Override
    public UserRecommendations insert(UserRecommendations userRecommendations) {
        this.userRecommendationsDao.insert(userRecommendations);
        return userRecommendations;
    }
    /**
     * 修改数据
     *
     * @param userRecommendations 实例对象（包含要更新的字段和主键）
     * @return 更新后的实例对象，如果更新失败则返回null或抛出异常
     * @throws IllegalArgumentException 如果userRecommendations对象为null或主键字段为空
     * @throws DataAccessException 如果数据库操作失败
     */
    @Override
    public UserRecommendations update(UserRecommendations userRecommendations) throws IllegalArgumentException {
        if (userRecommendations == null || userRecommendations.getUserId() == null) {
            throw new IllegalArgumentException("UserRecommendations object or userId cannot be null");
        }
        try {
            int updateCount = this.userRecommendationsDao.update(userRecommendations);
            if (updateCount > 0) {
                return this.userRecommendationsDao.queryById(userRecommendations.getUserId());
            } else {
                return null;
            }
        } catch (RuntimeException e) {
            throw new RuntimeException("Failed to update user recommendations", e);
        }
    }
    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userRecommendationsDao.deleteById(userId) > 0;
    }
}